//
//  AuctionPayType.swift
//  LeGou
//
//  Created by lxm on 2022/10/20.
//

import UIKit

class AuctionPayType: LemonBaseView {

    //MARK: --------------------属性--------------------
    let selectIndex = ZMUtil.fastBehavior(value: 0)
    //MARK: --------------------UI控件--------------------
    let top = ZMUtil.label(title: "竞购实际获得", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 14))
    let line = UIView().then { v in
        v.backgroundColor = Style.lineColor
    }
    let goodsBtn = UIButton().then { b in
        b.setImage(UIImage.init(named: "auction_check_u"), for: .normal)
        b.setImage(UIImage.init(named: "auction_check_s"), for: .selected)
        b.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
        b.contentEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 0)
        b.setTitleColor(UIColor.zm.color333333(), for: .normal)
        b.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        b.setTitle("商品", for: .normal)
    }
    
    let pointBtn = UIButton().then { b in
        b.setImage(UIImage.init(named: "auction_check_u"), for: .normal)
        b.setImage(UIImage.init(named: "auction_check_s"), for: .selected)
        b.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
        b.contentEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 0)
        b.setTitleColor(UIColor.zm.color333333(), for: .normal)
        b.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        b.setTitle("积分", for: .normal)
    }
    
    let hckBtn = UIButton().then { b in
        b.setImage(UIImage.init(named: "auction_check_u"), for: .normal)
        b.setImage(UIImage.init(named: "auction_check_s"), for: .selected)
        b.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
        b.contentEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 0)
        b.setTitleColor(UIColor.zm.color333333(), for: .normal)
        b.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        b.setTitle("加入合成库", for: .normal)
    }
    
    let pointSubLabel = ZMUtil.label(title: "-", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12))
    
    let bottomContent = UIView()
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        addSubview(top)
        addSubview(line)
        addSubview(bottomContent)
        bottomContent.addSubview(goodsBtn)
        bottomContent.addSubview(pointBtn)
        bottomContent.addSubview(pointSubLabel)
        bottomContent.addSubview(hckBtn)
        backgroundColor = .white
        hckBtn.titleLabel?.numberOfLines = 0
    }
    
    func addConstraints() {
        top.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(12)
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(top.snp.bottom).offset(12)
            make.height.equalTo(0.5)
        }
        
        bottomContent.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom)
            make.left.bottom.right.equalToSuperview()
        }
        
        goodsBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
        }
        
        pointBtn.snp.makeConstraints { make in
            make.left.equalTo(goodsBtn.snp.right).offset(8)
            make.centerY.equalToSuperview()
        }
        
        pointSubLabel.snp.makeConstraints { make in
            make.left.equalTo(pointBtn.snp.right).offset(4)
            make.centerY.equalTo(pointBtn)
        }
        
        hckBtn.snp.makeConstraints { make in
            make.left.equalTo(pointSubLabel.snp.right).offset(4)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-8)
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        selectIndex.subscribe(onNext:{ [unowned self] resp in
            self.goodsBtn.isSelected = resp == 0
            self.pointBtn.isSelected = resp == 1
            self.hckBtn.isSelected = resp == 2
        }).disposed(by: disposeBag)
        
        goodsBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.selectIndex.accept(0)
        }).disposed(by: disposeBag)
        
        pointBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.selectIndex.accept(1)
        }).disposed(by: disposeBag)
        
        hckBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.selectIndex.accept(2)
        }).disposed(by: disposeBag)
    }
    //MARK: --------------------delegate--------------------

}
